home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Delphi Informant Complete 1995 - 2000
/
Delphi Informant Complete 1995 to 2000.iso
/
Delphi Informant Magazine Complete Works SOURCE CODE 1998.rar
/
1998
/
Mar
/
di9803rs
/
SNDXALGS.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1997-08-20
|
4KB
|
161 lines
unit SndxAlgs;
interface
uses
SysUtils;
function Soundex(in_str : String) : String;
function NumericSoundex(in_str : String) : Smallint;
function ExtendedSoundex(in_str : String) : String;
implementation
// Calculate a normal Soundex encoding.
function Soundex(in_str : String) : String;
var
no_vowels, coded, out_str : String;
ch : Char;
i : Integer;
begin
// Make upper case and remove
// leading and trailing spaces.
in_str := Trim(UpperCase(in_str));
// Remove vowels, spaces, H, W, and Y,
// except for the first character.
no_vowels := in_str[1];
for i := 2 to Length(in_str) do
begin
ch := in_str[i];
case ch of
'A', 'E', 'I', 'O', 'U', ' ', 'H', 'W', 'Y':
; // Do nothing.
else
no_vowels := no_vowels + ch;
end;
end;
// Encode the characters.
for i := 1 to Length(no_vowels) do
begin
ch := no_vowels[i];
case ch of
'B', 'F', 'P', 'V':
ch := '1';
'C', 'G', 'J', 'K', 'Q', 'S', 'X', 'Z':
ch := '2';
'D', 'T':
ch := '3';
'L':
ch := '4';
'M', 'N':
ch := '5';
'R':
ch := '6';
else // Vowels, H, W, and Y as the 1st letter.
ch := '0';
end; // End case ch.
coded := coded + ch;
end; // End for i := 1 to Length(no_vowels) do
// Use the first letter.
out_str := no_vowels[1];
// Find three non-repeating codes.
for i := 2 to Length(no_vowels) do
begin
// Look for a non-repeating code.
if (coded[i] <> coded[i - 1]) then
begin
// This one works.
out_str := out_str + coded[i];
if (Length(out_str) >= 4) then Break;
end;
end;
Soundex := out_str;
end;
// Calculate a numeric Soundex encoding.
function NumericSoundex(in_str : String) : Smallint;
var
value : Integer;
begin
// Calculate the normal Soundex encoding.
in_str := Soundex(in_str);
// Convert this into a numeric value.
value := (Ord(in_str[1]) - Ord('A')) * 1000;
if (Length(in_str) > 1) then
value := value +
StrToInt(Copy(in_str, 2, Length(in_str) - 1));
NumericSoundex := value;
end;
// Calculate an extended Soundex encoding.
function ExtendedSoundex(in_str : String) : String;
// Replace instances of fr_str with to_str in str.
procedure ReplaceString(var str : String;
fr_str, to_str : String);
var
fr_len, i : Integer;
begin
fr_len := Length(fr_str);
i := Pos(fr_str, str);
while (i > 0) do
begin
str :=
Copy(str, 1, i - 1) +
to_str +
Copy(str, i + fr_len, Length(str) - i - fr_len + 1);
i := Pos(fr_str, str);
end;
end;
var
no_vowels : String;
ch, last_ch : Char;
i : Integer;
begin
// Make upper case and remove
// leading and trailing spaces.
in_str := Trim(UpperCase(in_str));
// Remove internal spaces.
ReplaceString(in_str, ' ', '');
// Convert CHR to CR.
ReplaceString(in_str, 'CHR', 'CR');
// Convert PH to F.
ReplaceString(in_str, 'PH', 'F');
// Convert Z to S.
ReplaceString(in_str, 'Z', 'S');
// Remove vowels and repeats.
last_ch := in_str[1]; // The last character used.
no_vowels := last_ch;
for i := 2 to Length(in_str) do
begin
ch := in_str[i];
case ch of
'A', 'E', 'I', 'O', 'U':
; // Do nothing.
else
// Skip it if it's a duplicate.
if (ch <> last_ch) then
begin
no_vowels := no_vowels + ch;
last_ch := ch;
end;
end;
end;
ExtendedSoundex := no_vowels;
end;
end.